package com.ciba.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ciba.demo.mapper.BookInfoMapper;
import com.ciba.demo.mapper.UserInfoMapper;
import com.ciba.demo.mapper.UserPlanMapper;
import com.ciba.demo.pojo.BookInfo;
import com.ciba.demo.pojo.UserInfo;
import com.ciba.demo.pojo.UserPlan;
import com.ciba.demo.respone.ApiRestResponse;
import com.ciba.demo.service.PlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Repository
public class PlanServiceImpl implements PlanService {

    private int CET4START = 1 ;
    private int CET6START = 3163 ;

    @Autowired
    UserInfoMapper userInfoMapper ;

    @Autowired
    UserPlanMapper userPlanMapper ;

    @Autowired
    BookInfoMapper bookInfoMapper ;

    @Override
    public UserPlan getPlan(UserInfo user){
        QueryWrapper<UserPlan> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",user.getId()) ;
        return userPlanMapper.selectOne(wrapper) ;
    }

    @Override
    public ApiRestResponse getPlan(HttpServletRequest request, HttpServletResponse response) {
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("user") ;
        if(userInfo == null){
            return ApiRestResponse.error().setStatus(-1).setMsg("没登录") ;
        }
        int nowPlan = userInfo.getNow_plan() ;
        if(nowPlan == 0 ){
            return ApiRestResponse.error().setStatus(0).setMsg("没计划") ;
        }else{
            QueryWrapper<UserPlan> wrapper = new QueryWrapper<>() ;
            UserPlan userPlan = userPlanMapper.selectById(nowPlan) ;
            if(userPlan!=null){
                return ApiRestResponse.success().setData(userPlan).setMsg("获取成功").setStatus(1) ;
            }else{
                return ApiRestResponse.error().setMsg("服务器炸了").setStatus(-1) ;
            }
        }
    }

    @Override
    public ApiRestResponse setPlan(HttpServletRequest request, HttpServletResponse response) {
        UserInfo userInfo =  (UserInfo) request.getSession().getAttribute("user") ;
        int planId = userInfo.getNow_plan();
        String book = request.getParameter("book") ;
        QueryWrapper<BookInfo> wrapper = new QueryWrapper<>() ;
        wrapper.eq(true,"name",book) ;
        BookInfo b = bookInfoMapper.selectOne(wrapper);
        String reviewWordNub = request.getParameter("reviewWordNub") ;
        String reciteWordNub = request.getParameter("reciteWordNub") ;
        int days = Integer.parseInt(request.getParameter("days")) ;
        UserPlan userPlan = new UserPlan() ;
        userPlan.setBook(b.getId()) ;
        userPlan.setRecite(Integer.parseInt(reciteWordNub));
        userPlan.setReview(Integer.parseInt(reviewWordNub));
        userPlan.setDays(days);
        userPlan.setUserId(userInfo.getId());
        UpdateWrapper<UserPlan> wrapper_2 = new UpdateWrapper<>() ;
        wrapper.eq(true,"user_id",userInfo.getId()) ;
        int ok = userPlanMapper.update(userPlan,wrapper_2);
        if(ok>=1){
            return  ApiRestResponse.success().setStatus(1).setMsg("修改成功");
        }else{
            addPlan(request,response);
            return  ApiRestResponse.error().setMsg("修改失败").setStatus(0);
        }
    }

    @Override
    public ApiRestResponse getPlans(HttpServletRequest request, HttpServletResponse response) {
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("user") ;
        QueryWrapper<UserPlan> wrapper = new QueryWrapper<>() ;
        wrapper.eq("user_id",userInfo.getId()) ;
        List<UserPlan> list = userPlanMapper.selectList(wrapper) ;
        if(list.isEmpty()){
            return ApiRestResponse.error().setStatus(0).setMsg("该用户没计划！");
        }else{
            return ApiRestResponse.success().setStatus(1).setMsg("返回成功").setData(list);
        }
    }

    @Override
    public ApiRestResponse addPlan(HttpServletRequest request, HttpServletResponse response) {
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("user") ;
        String book = request.getParameter("book");
        QueryWrapper<BookInfo> wrapper = new QueryWrapper<>() ;
        wrapper.eq(true,"name",book) ;

        BookInfo b = bookInfoMapper.selectOne(wrapper);
        int reviewWordNub = Integer.parseInt(request.getParameter("reviewWordNub")) ;
        int reciteWordNub = Integer.parseInt(request.getParameter("reciteWordNub")) ;
        int days = Integer.parseInt(request.getParameter("days")) ;

        UserPlan userPlan = new UserPlan() ;
        userPlan.setUserId(userInfo.getId());
        userPlan.setReview(reviewWordNub);
        userPlan.setRecite(reciteWordNub);
        userPlan.setBook(b.getId());
        userPlan.setDays(days);
        userPlan.setNextWord(b.getId()==1 ? CET4START : CET6START);
        int res = userPlanMapper.insert(userPlan);

        if(res==1){
            return ApiRestResponse.success().setStatus(1).setMsg("正确");
        }else{
            return ApiRestResponse.error().setStatus(0).setMsg("error") ;
        }
    }
}
